package com.example.chenhaosblog.controller;

import com.example.chenhaosblog.base.BaseController;
import com.example.chenhaosblog.bean.User;
import com.example.chenhaosblog.service.IUserService;
import com.example.chenhaosblog.utils.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController extends BaseController {

    @Resource
    private IUserService userInfoService;

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }


    @RequestMapping({"/","/font"})
    public String font(){
        return "font";
    }

    @RequestMapping({"/login"})
    public String toIndex(){
        return "login";
    }

    @Value("${getImg.url}")
    private String getimg;//图片访问路径

    @RequestMapping("/toLogin")
    @ResponseBody
    public PageData login(HttpServletRequest request, Map<String, Object> map, HttpServletResponse response,String username,String password) throws Exception{
        PageData result = new PageData();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
         subject.login(token);
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute(username);
            result.put("code", "00");
            System.out.println(user.getHead_img());
            if(user.getHead_img()!=null && !"".equals(user.getHead_img())){
                result.put("imgsrc",getimg+user.getHead_img());
            }else {
                result.put("imgsrc",getimg+"deft.png");
            }

            result.put("data", user);
            result.put("msg", "登陆成功");
        } catch (AuthenticationException e) {
            throw new Exception(e.toString());
        }

        // 此方法不处理登录成功,由shiro进行处理

        return result;
    }




    @RequestMapping("/loginOut")
    @ResponseBody
    public PageData singleUseLogin() throws Exception {
        PageData result = new PageData();

        try {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            result.put("code", "00");
            result.put("data", "");
            result.put("msg", "退出成功");
        } catch (Exception e) {
            throw new Exception(e.toString());
        }


        return result;
    }


    @RequestMapping("/loginInfo")
    @ResponseBody
    public PageData loginInfo() throws Exception {
        PageData result = new PageData();
        try {
            User data= (User) SecurityUtils.getSubject().getPrincipal();
            if(data!=null){
                result.put("code", "00");
                result.put("data", data);
                result.put("msg", "查询成功");
            }else{
                result.put("code", "01");
                result.put("data", "");
                result.put("msg", "未登录");
            }

        } catch (Exception e) {
            throw new Exception(e.toString());
        }

        return result;
    }


    @RequestMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "403";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public PageData upload() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        return result;
    }


}